
* +++++++++++++++++++++++++++++
* TABLE 7 + 8: 

* TABLE 7:
* INTEGRATION COURSES AND
* TEACHER UNEMPLOYMENT RATES


* TABLE 8:
* IV ESTIMATES
* MEASURES OF INTEGRATION AND
* INTEGRATION COURSES
* +++++++++++++++++++++++++++++


* load data
use "${data_derived}/regional_analysis_data.dta",clear

* measures of integration that should be LHS in IV table (code takes logs)
local int_vars "integration gen_friendliness relative_friending language frac_emp_train_any_syria"

* shorten some variable names, otherwise they get to long when we create logs
ren *teach* *tch*
ren *gen_schl* *gen_sc*
ren *unemp_uni* *unemp_he*

* create logs
foreach var of varlist n_frnd_nat_lcl_sy_nat_avg_re n_frnd_nat_lcl_sy_avg_re ///
	n_frnd_nat_lcl_native_avg_re produ_any_de_sy_avg_re frac_emp_train_any_syria ///
	unemp_* {
	cap gen log_`var' = log(`var')
	ren `var' raw_`var'

}

* give FB integration measures better variable names
gen log_integration        = log_n_frnd_nat_lcl_sy_avg_re
gen log_gen_friendliness   = log_n_frnd_nat_lcl_native_avg_re
gen log_relative_friending = log_n_frnd_nat_lcl_sy_nat_avg_re
gen log_language           = log_produ_any_de_sy_avg_re

* controls iv regressions
local control_iv "avg_age log_pop_dens_2018 log_avg_inc log_unemp_tot2014_per_pop log_unemp_tot2014_per_syr log_pos_per_app_post"
	  
* potential instruments 
local pot_ivs "log_unemp_tch_oth2014_per_syr log_unemp_gen_sc2014_per_syr log_unemp_vocat2014_per_syr log_unemp_drv_sprt2014_per_syr"

* selected instrument
local iv "log_unemp_tch_oth2014_per_syr"	  
	   
* first stage regressions (Table 7)
* note: also runs second stage regression just to get the F-stat
eststo clear
local i = 1
foreach pot_iv in `pot_ivs' {
	ivreg2 log_integration `control_iv' ///
		(log_per_syr_cours_compl_15_19 = `pot_iv') [w=n_frnd_nat_lcl_sy_n], robust
	local f_stat = `e(cdf)'
	eststo fs`i' : reg log_per_syr_cours_compl_15_19 `control_iv' `pot_iv' [w=n_frnd_nat_lcl_sy_n], robust
	estadd local f_stat `f_stat'
	local ++i
}

esttab * using "${output}/iv_first_stage.csv", replace ///
	cells(b(star fmt(3)) se(par fmt(2))) legend ///
	starlevels(* 0.10 ** 0.05 *** 0.010) ///
	stats(f_stat r2 N mean, labels("CD F-Stat" "R-squared")) label keep(log_unemp*)

* second stage regressions (Table 8)
* loop over different outcomes
eststo clear
local i = 1
foreach int_var in `int_vars' { 
	if "`int_var'" == "gen_friendliness"  		local weights "n_frnd_nat_lcl_native_n"
	else  										local weights "n_frnd_nat_lcl_sy_n"
	eststo iv_reg`i' : ivreg2 log_`int_var' `control_iv' ///
		(log_per_syr_cours_compl_15_19 = `iv') [w=`weights'], robust
	local ++i
}

esttab * using "${output}/iv_second_stage.csv", replace ///
	cells(b(star fmt(3)) se(par fmt(2))) legend ///
	starlevels(* 0.10 ** 0.05 *** 0.010) ///
	stats(r2 N mean, labels("R-squared")) label ///
	keep(log_per_syr_cours_compl_15_19)
